package cn.zhizihua.query;

import cn.zhizihua.pojo.Cost;
import cn.zhizihua.pojo.Register;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.Data;

import java.util.List;

@Data
public class CostQuery extends BaseQuery{

    private String caseNumber;
    private Integer registerId;
    /*
    费用表和挂号表之间的联查
     */
    @Override
    public Wrapper getQueryWrapper() {
        MPJLambdaWrapper<Cost> wrapper = new MPJLambdaWrapper<>();
        wrapper.setAlias("c")
                .selectAll(Cost.class,"c")
                .selectAs(Register::getCaseNumber,"'register.caseNumber'")
                .selectAs(Register::getRealName,"'register.realName'")
                .selectAs(Register::getCardNumber,"'register.cardNumber'")
                .leftJoin(Register.class,"r",Register::getId,Cost::getRegisterId)
                .orderByDesc("c.id");
        if (StringUtils.isNotEmpty(caseNumber)){
            wrapper.eq("r.case_number",caseNumber);
        }
        return wrapper;
    }


    /**
     * 跟修改关联的条件
     */
    private String payMethod;
    private Integer settleCategoryId;
    private Integer state;
    private List<Integer> ids;

    //update cost set state=1,pay_method='现金',setttle_category_id=1 where id in (1,2,3,4);
    public UpdateWrapper<Cost> getUpdateWrapper(){
        UpdateWrapper<Cost> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(StringUtils.isNotEmpty(payMethod),"pay_method",payMethod)
                .set(settleCategoryId!=null,"settle_category_id",settleCategoryId)
                .set(state!=null,"state",state)
                .in("id",ids);
        return updateWrapper;
    }

}